package com.company.genealogy.web.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.genealogy.common.constant.ApiCodeEnum;
import com.company.genealogy.common.result.ApiResponse;
import com.company.genealogy.web.entity.Clan;
import com.company.genealogy.web.service.ClanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/clans")
public class ClanController {


    @Autowired
    private ClanService clanService;

    // 分页查询家族列表
    @GetMapping
    public ApiResponse<IPage<Clan>> list(@RequestParam(defaultValue = "1") int page,
                                         @RequestParam(defaultValue = "10") int size) {
        IPage<Clan> result = clanService.page(new Page<>(page, size));
        return ApiResponse.success(ApiCodeEnum.SUCCESS, result);
    }

    // 新增家族
    @PostMapping
    public ApiResponse<Clan> create(@RequestBody Clan clan) {
        clanService.save(clan);
        return ApiResponse.success(ApiCodeEnum.SUCCESS, clan);
    }

    // 家族详情
    @GetMapping("/{id}")
    public ApiResponse<Clan> detail(@PathVariable Long id) {
        Clan clan = clanService.getById(id);
        return ApiResponse.success(ApiCodeEnum.SUCCESS, clan);
    }

    // 编辑家族
    @PutMapping("/{id}")
    public ApiResponse<Void> update(@PathVariable Long id, @RequestBody Clan clan) {
        clan.setId(id);
        clanService.updateById(clan);
        return ApiResponse.success(ApiCodeEnum.SUCCESS, null);
    }

    // 删除家族
    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@PathVariable Long id) {
        clanService.removeById(id);
        return ApiResponse.success(ApiCodeEnum.SUCCESS, null);
    }
} 